package com.example.gybicycle.common.util;

import  com.alibaba.fastjson.JSON;
import  com.alibaba.fastjson.JSONObject;
import com.ctg.ag.sdk.biz.AepDeviceCommandClient;
import com.ctg.ag.sdk.biz.aep_device_command.*;
import com.example.gybicycle.common.bigcycle.OpenLock;
import com.example.gybicycle.common.entity.Result;

public class BiCycleUtil {

    public static final String  APPKEY="KfDSqx4IRG5";
    public static final String APPSecret="pF1AsNpuru";
    public static final String MasterKey="e606ca593818449384bc6d6ba4756cde";
    public static  final Integer ProductId=17092946;

    /**
     * 开锁
     * @param childLockId 子锁id
     * @param mainLockId 主锁id
     * @param operator 开锁人
     * @return
     */
    public static boolean openLock(String childLockId, String mainLockId,String operator){
//        Integer childLockId,String mainLockId,String operator,Integer productId
        try{

            OpenLock openLock=new OpenLock(Integer.valueOf(childLockId),mainLockId,operator,ProductId);
            String data=JSON.toJSONString(openLock);
            AepDeviceCommandClient client = AepDeviceCommandClient.newClient()
                    .appKey(APPKEY).appSecret(APPSecret)
                    .build();
            CreateCommandRequest request = new CreateCommandRequest();
            request.setParamMasterKey(MasterKey);	// single value
            request.setBody(data.getBytes());
            CreateCommandResponse response=client.CreateCommand(request);
            if(response.getStatusCode()!=200)
                return false;
            return true;
//            System.out.println();
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }

    }
    public static void main(String[] args)   {
        try{
        /*    AepDeviceCommandClient client = AepDeviceCommandClient.newClient()
                    .appKey(APPKEY).appSecret(APPSecret)
                    .build();
             CreateCommandRequest request = new CreateCommandRequest();
            // set your request params here
            request.setParamMasterKey(MasterKey);	// single value
            request.setBody("{\"content\":{\"params\":{\"SLN\":3},\"serviceIdentifier\":\"ULC\"},\"deviceId\":\"17092946867303075486498\",\"operator\":\"huazhu\",\"productId\":17092946,\"ttl\":60,\"level\":1}".getBytes());	//具体格式见前面请求body说明
            System.out.println(client.CreateCommand(request));*/
//            /*openLock(3,"7303075486498","测试");
            openLock("5","7303075486498","测试");
//            openLock(1,"7303075486498","测试");
//            openLock(4,"7303075486498","测试");*/

            String re="{\"upPacketSN\":-1,\"upDataSN\":-1,\"topic\":\"ad\",\"timestamp\":1718595550179,\"tenantId\":\"2000036795\",\"serviceId\":\"data_report\",\"protocol\":\"mqtt\",\"productId\":\"17092946\",\"payload\":{\"state\":\"0\",\"signalStrength\":31,\"seq\":227,\"openType\":2,\"batteryLevel\":134,\"SN\":\"D001038182187119\",\"NO\":\"7303075486498\"},\"messageType\":\"dataReport\",\"deviceType\":\"\",\"deviceId\":\"17092946867303075486498\",\"assocAssetId\":\"\",\"IMSI\":\"\",\"IMEI\":\"\"}";
            String ss=" {\"timestamp\":1718595546958,\"tenantId\":\"2000036795\",\"taskId\":15,\"result\":{\"resultDetail\":\"\",\"resultCode\":\"SENT\"},\"protocol\":\"mqtt\",\"productId\":\"17092946\",\"messageType\":\"commandResponse\",\"deviceId\":\"17092946867303075486498\"}";
            JSONObject object=JSON.parseObject(re);
//            JSONObject object=JSON.parseObject(ss);
            if(object.get("payload")==null){
                System.out.println("非开关锁记录");
            }else {
                 JSONObject object1=object.getJSONObject("payload");
                Integer state=object1.getInteger("state");
                String mainId=object1.getString("NO");
                String SN=object1.getString("SN");
                if(mainId==null||SN==null||state==null)
                    return  ;
                String cid=SN.substring(1,4);
                int number = Integer.parseInt(cid);
                String latchNo=SN.substring(4);
                if(state==0) {
//                开锁

                }else if(state==1){
//                关锁

                }
            }

            System.out.println(object);
//            System.out.println(object2);
//1,3,2,5,4


            // more requests


        }catch (Exception e){
            e.printStackTrace();
        }

    }
}
